package com.cloud.stock;

import com.cloud.controller.TestController;
import org.springframework.util.CollectionUtils;

import java.io.File;
import java.util.*;

/**
 * @author: create by God of SJF
 * @version: v1.0
 * @description: com.cloud.stock
 * @date:2021/9/17
 */
public class 按数量分文件3 {

    public static void main(String[] args) throws Exception {
        File file = new File("/Users/libin/Desktop/码流转3/");

        File[] fileList = file.listFiles();
        Arrays.sort(fileList, new Comparator<File>() {
            public int compare(File f1, File f2) {
                long diff = f1.lastModified() - f2.lastModified();
                if (diff > 0)
                    return 1;
                else if (diff == 0)
                    return 0;
                else
                    return -1;//如果 if 中修改为 返回-1 同时此处修改为返回 1  排序就会是递减
            }

            public boolean equals(Object obj) {
                return true;
            }

        });
        int f = 0;
        for (File thisFile : fileList) {
            System.out.println(thisFile.getName());
            int a = 0;
            Scanner scanner = new Scanner(thisFile);
            List<String> list = new ArrayList<String>();
            while (scanner.hasNextLine()) {
                if (a == 100000) {
                    File file2 = new File("/Users/libin/Desktop/码流转4/" + f);
                    a = 0;
                    f++;
                    TestController.writeFile(file2.getAbsolutePath(), list);
                    list = null;
                    list = new ArrayList<String>();
                }
                String line = scanner.nextLine();
                list.add(line + "\r\n");
                a++;
            }
            if(!CollectionUtils.isEmpty(list)){
                File file2 = new File("/Users/libin/Desktop/码流转4/" + f);
                a = 0;
                f++;
                TestController.writeFile(file2.getAbsolutePath(), list);
                list = null;
                list = new ArrayList<String>();
            }
            thisFile.delete();
        }
    }
}
